var e = getApp(),
  c = e.requirejs("api/index"),
  r = e.requirejs("core");
Page({
  data: {
    menu: e.tabBar,
    menu_show: false,
    showtabbar_back: false,
    member: {

    },
    haslogin:false,
    dobuy: false,

    now_sku: {},
    num: 1,
    nowprice: 0.00,
    skus: [],
    isinputing: false,

  },
  //底部导航跳转
  menu_url: function (k) {
    r.menu_url(k, 2);
  },

  //跳转+收集表单id
  formSubmit: function (k) {
    var vv = { currentTarget: { dataset: {} } },
      data = k.detail.value;
    vv.currentTarget.dataset = data;
    r.menu_url(vv, 1);

    var d = {}
    d.formid = k.detail.formId;
    d.openid = getApp().getCache("userinfo").openid;
    d.username = getApp().getCache("userinfo").nickName;
    r.get('Market/getFormid', d, function (t) {
      console.log(t)
    })
  },

  //跳转
  to_url: function (k) {
    r.menu_url(k, 1);
  },

  onLoad: function (options) {
    if (options != null && options != undefined) {
      this.setData({
        tabbar_index: options.tabbar_index ? options.tabbar_index : -1
      });
    }

    var showtabbar = this.data.tabbar_index >= 0;
    this.setData({
      menu: getApp().tabBar,
      showtabbar: showtabbar,
      showtabbar_back: showtabbar
    })

  },

  onGotUserInfo: function (q) {
    console.log(q)
    var that = this,
      user = e.getCache("userinfo");
    if (user) {
      return;
    }

    var user = q.detail.userInfo;
    var dd = {};
    dd.haslogin = user ? true : false;
    if (user) {
      dd.member = user;
    }
    console.log(dd);
    that.setData({
      dd
    })

    e.getUserInfo(q.detail.userInfo, function (tt) {
      console.log("tt !!!!!!!!!!!!!");
           console.log(tt);

      that.getInfo();

    }, q.detail.encryptedData, q.detail.iv);
    
  },

  getInfo: function () {
    var that = this;
    var user = getApp().getCache("userinfo");
    if (!user){
      return;
    }

    that.setData({
      member: user,
      haslogin:true,
    })
    
    r.get("paycontent/uinfo", {
      uid: user.uid,
    }, function (r) {
      console.log(r)
      if(r.code == 1)
      {
        var member = that.data.member;
        member.expire_day = r.info.expire_day;
        member.isvip = r.info.isvip;
        console.log(member);
        that.setData({
          member: member
        })
      }
    })
  },

  getallprice: function () {
    var a = this, id = a.data.id;
    var e = {};
    r.get("paycontent/allprice", {}, function (t) {
      if (t.code == 0) {

        if (t.info.length > 0) {
          e.skus = t.info;
          e.now_sku = t.info[0];
          e.nowprice = t.info[0].price;
          a.setData(e);
        }

      } else {
        r.alert(t.msg)
      }
    }, true)
  },


  inputing: function (e) {
    console.log(e);
    var isinputing = this.data.isinputing;
    console.log(isinputing);

    this.setData({
      isinputing: !isinputing
    })
  },

  dobuy: function (e) {
    if (this.data.skus.length > 0) {
      this.setData({
        dobuy: true,
        showtabbar:false,
      })
    }
    else {
      core.alert("未添加资费,暂不可购买");
    }

  },

  hidebuy: function (e) {
    var that = this;
    if (!this.data.isinputing) {
      this.setData({
        dobuy: false,
        showtabbar: that.data.showtabbar_back,
      })
    }
  },

  choose_sku: function (e) {

    console.log(e);
    var index = e.currentTarget.dataset.index;
    var sku = this.data.skus[index];

    var nowprice = (sku.price * this.data.num).toFixed(2);

    this.setData({
      now_sku: sku,
      nowprice: nowprice
    })

  },

  num_change: function (e) {

    console.log(e);
    var num = e.detail.value;
    var nowprice = (this.data.now_sku.price * num).toFixed(2);
    this.setData({
      num: num,
      nowprice: nowprice
    })
  },


  tobuy: function (e) {

    if (this.data.num <= 0) {
      r.error("购买数量必须大于0");
      return
    }
    var that = this;
    this.setData({
      dobuy: false,
      showtabbar: that.data.showtabbar_back,
    });

    r.loading("支付中...");
    
    var data = {};
    data.price_id = this.data.now_sku.id;
    data.num = this.data.num;
    data.uid = getApp().getCache('userinfo').uid;
    data.openid = getApp().getCache("userinfo").openid;

    r.get("paycontent/createOrder", data, function (t) {
      if (0 == t.code) {

        wx.requestPayment({
          'timeStamp': t.info.timeStamp,
          'nonceStr': t.info.nonceStr,
          'package': t.info.package,
          'signType': 'MD5',
          'paySign': t.info.paySign,
          'success': function (res) {

            console.log(res)
            if (res.errMsg == "requestPayment:ok") {

              wx.showToast({
                title: "支付成功！",
                icon: "success",
                duration: 2000,
              })

               setTimeout(function () {
                that.getInfo();
                that.getallprice();
              }, 2000);

            } else {
              r.error('支付失败！');
            }
          },
          'fail': function (res) {
            r.error('您已经取消支付！')
          }
        })

      } else {
        r.error(t.msg)
      }
    });

  },

  onShow: function () {
    this.getInfo();
    this.getallprice();
  },

  relogin: function () {
    var that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting['scope.userInfo']) {
                e.getUserInfo();
                setTimeout(function () {
                  that.getInfo()
                }, 1e3)

              }
            }
          })
        }
      }
    })

  },


})